#ifndef MSNETWORK_H
#define MSNETWORK_H
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#if defined(OS_ANDROID)||defined(OS_LINUX_SOC)
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include <linux/tcp.h>
#endif
#include <libmslog/mstype.h>
#include "mscommon.h"


typedef unsigned long nfds_t;
struct pollfd {
    ms_s32 fd;
    ms_s16 events;  /* events to look for */
    ms_s16 revents; /* events that occurred */
};

/* events & revents */
#define POLLIN     0x0001  /* any readable data available */
#define POLLOUT    0x0002  /* file descriptor is writeable */
#define POLLRDNORM POLLIN
#define POLLWRNORM POLLOUT
#define POLLRDBAND 0x0008  /* priority readable data */
#define POLLWRBAND 0x0010  /* priority data can be written */
#define POLLPRI    0x0020  /* high priority readable data */

/* revents only */
#define POLLERR    0x0004  /* errors pending */
#define POLLHUP    0x0080  /* disconnected */
#define POLLNVAL   0x1000  /* invalid file descriptor */

//ms
#define msnet_default_timeout_send	1000
#define msnet_default_timeout_recv	1000
#define msnet_default_timeout_rw	1000

#define poll msnet_api_poll

#define MIN_PORT	1024   /* minimum port allowed */
#define MAX_PORT 65535  /* maximum port allowed */

#define msnet_maxlen_ifname	64
#define msnet_maxlen_mac		18	//2c:53:4a:01:11:2a
#define msnet_maxlen_mtu		16
#define msnet_maxlen_ip		16
#define msnet_maxlen_mask		16
#define msnet_maxlen_gw		16
#define msnet_maxnum_ipout				64


#define msnet_checktime_ipping		30
typedef enum {
	msnet_tcp_unkonw = 0,
	msnet_tcp_established = 1,
	msnet_tcp_synsend,
	msnet_tcp_synrecv,
	msnet_tcp_finwait1,
	msnet_tcp_finwait2,
	msnet_tcp_timewait,
	msnet_tcp_close,
	msnet_tcp_closewait,
	msnet_tcp_lastack,
	msnet_tcp_listen,
	msnet_tcp_closing,	/* Now a valid state */
	msnet_tcp_maxstates	/* Leave at the end! */
}MSNETTCPState;
typedef enum NETCFG_MODE{
	netcfg_mode_unknow=0x00,
	netcfg_mode_static,
	netcfg_mode_dhcp,
	netcfg_mode_bondslaver,
	netcfg_mode_bondmaster,
	netcfg_mode_max,
}NETCFG_MODE;
#define ethnmode_string \
	 "unkonw", \
	 "static", \
	 "dhcp", \
	 "bond-slaver", \
	 "bond-master", \
	 "max"
	 
typedef struct NETCFGInfo{
	ms_u08   name[msnet_maxlen_ifname];
	ms_u08   nameAnother[msnet_maxlen_ifname];//别名,方便前端页面名称显示;
	ms_u08   address[msnet_maxlen_mac];
	ms_u08   ipaddress[msnet_maxlen_ip];
	ms_u08   netmask[msnet_maxlen_mask];
	ms_u08   netmaskbit[64];
	ms_u08   gateway[msnet_maxlen_gw];
	ms_u08   usrgateway[msnet_maxlen_gw];
	ms_u08   mtu[msnet_maxlen_mtu];
	ms_u08   bondmaster_name[msnet_maxlen_mtu];
	ms_u08   bondslave_name[msnet_maxlen_mtu*20];
	ms_u08   dns1[128];
	ms_u08   dns2[128];
	ms_u08   mode[128];
	ms_bool flag_defaultgw;
	
	ms_bool use_pppoe;
	ms_bool flag_needRestart;
	ms_bool flag_wifi;
}NETCFGInfo;

typedef struct MSIPlist{
	ms_u08   totalnum;
	ms_u08   ipaddr[128][32];	
	ms_bool  flag_ipv6[32];	
}MSIPlist;

typedef struct MSIPPINGContext{
	ms_u08 num;
	ms_u08 list[64][32];
	ms_bool flag_reachable[64];
	ms_bool flag_multicast[64];
	ms_bool info[64][32];
	ms_bool cmd_stop;
}MSIPPINGContext;

#ifndef MSNETWORK_C
	#define MSNET_INTERFACE extern
#else
	#define MSNET_INTERFACE
#endif
MSNET_INTERFACE ms_s08 
	msnet_api_init(ms_void);
MSNET_INTERFACE ms_void 
	msnet_api_deinit(ms_void);
MSNET_INTERFACE ms_bool 
	msnet_api_isReachable(ms_string ms_in paddr,ms_s32 port);	//"www.baidu.com"  
MSNET_INTERFACE ms_s32 
	msnet_api_socketNonblock(ms_s32 ms_io socket, ms_bool ms_in enable);
MSNET_INTERFACE ms_s08 
	msnet_api_socketConnect(ms_s32 ms_io  socket, struct sockaddr * ms_in plisten_addr,
		ms_u32 ms_in len,ms_u64 ms_in timeout,ms_bool flag_pint);
MSNET_INTERFACE ms_s32 
	msnet_api_poll(struct pollfd * ms_in fds, nfds_t ms_in numfds, ms_u32 ms_in timeout);
MSNET_INTERFACE ms_s32 
	msnet_api_waitFd(ms_s32 ms_in fd, ms_bool ms_in is_write);
MSNET_INTERFACE ms_s32 
	msnet_api_waitFdTimeout(ms_s32 ms_in fd, ms_s32 ms_in is_write,ms_s32 ms_in retrytime);
MSNET_INTERFACE ms_s08 
	msnet_api_getIfaceInfo(NETCFGInfo * ms_out pnetcfg_info) ;  
MSNET_INTERFACE ms_s08 
	msnet_api_setIfaceInfo(NETCFGInfo *  ms_in pnetcfg_info) ;   
MSNET_INTERFACE ms_bool 
	msnet_api_isMulticast( ms_string ms_in paddr);
MSNET_INTERFACE ms_void 
	msnet_api_getIpaddr(struct sockaddr *addr,ms_byte *ipaddrbuf);
MSNET_INTERFACE ms_bool 
	msnet_api_isResIpaddr( ms_string ms_in pipaddr);
MSNET_INTERFACE ms_s08 
	msnet_api_parseDomainName (ms_string ms_in phostname, ms_string ms_in pservice,MSIPlist *pmsiplist);
MSNET_INTERFACE ms_bool 
	msnet_api_isIpv6(ms_string ms_in ipaddr);
MSNET_INTERFACE ms_void 
	msnet_api_outIpv6(ms_pbyte ms_in ipaddr);
MSNET_INTERFACE ms_s08 
	msnet_api_getIfaceIp(const ms_string ms_in pifname, ms_string ms_out pipaddr);
MSNET_INTERFACE ms_s08 
	msnet_api_getIfaceIpMask(const ms_string ms_in pifname, ms_string ms_out pipaddr, ms_string ms_out pnetmask);
MSNET_INTERFACE ms_s08 
	msnet_api_getIfaceMac_string(ms_string ms_in pifname,ms_string ms_out pmacstr,ms_u32 ms_in size)  ;
MSNET_INTERFACE ms_s08 
	msnet_api_getIfaceMac_byte(ms_string ms_in pifname, ms_pbyte ms_out pmac);
MSNET_INTERFACE ms_s08 
	msnet_api_getIfaceMtu(ms_string ms_in pifname,ms_string ms_in mtubuf,ms_u32 ms_in size)  ;
MSNET_INTERFACE ms_s08 
	msnet_api_setIfaceMtu(ms_string ms_in pifname,ms_u32 ms_in mtuval)  ;
MSNET_INTERFACE ms_s08 
	msnet_api_setIfaceFlags( ms_s32 ms_in fd  ,ms_string ms_in pifname, ms_u32 ms_in flags) ;
MSNET_INTERFACE ms_void 
	msnet_api_macByte2str(ms_pbyte pbuf,ms_u32 len,ms_pbyte pstrout);
MSNET_INTERFACE ms_s08 
	msnet_api_macStr2Byte( ms_string ms_in pmacstr, ms_string ms_out poutbuf);
MSNET_INTERFACE ms_bool 
	msnet_api_isVaildMac( ms_string ms_in pmacstr);
MSNET_INTERFACE ms_void 
	msnet_api_getMulticastDestMac(ms_string destMac,ms_string pmaddr);
MSNET_INTERFACE ms_u32 
	msnet_api_ipStr2Byte( ms_string ms_in pipstr);
MSNET_INTERFACE ms_void 
	msnet_api_ipByte2Str( ms_u32 ms_in ipaddr_num, ms_string ms_out poutbuf);
MSNET_INTERFACE ms_u16 
	msnet_api_checksum(ms_pu16 ms_in paddr,ms_u32 ms_in bytes);
MSNET_INTERFACE ms_u16 
	msnet_api_checksumUdp(ms_pu16 ms_in paddr,ms_u32 ms_in bytes);
MSNET_INTERFACE ms_u16 
	msnet_api_checksum_ptc(ms_pbyte ms_in  pether_pkt,ms_u32  source_ip,ms_u32  dest_ip,ms_u08  protocol,ms_u08  header_len,ms_pbyte data,ms_u16 data_len);	
MSNET_INTERFACE ms_void 
	msnet_api_getTcpStateString( MSNETTCPState ms_in msnet_tcpstate,ms_string ms_out outstr) ;
MSNET_INTERFACE ms_s08 
	msnet_api_bindIface(ms_s32 ms_in fd , ms_string ms_in pifname) ;
MSNET_INTERFACE ms_bool 
	msnet_api_isTcpStateOK(ms_s32  	    fd , struct tcp_info* ptcp_info,ms_byte *pstr_tcpi_state);
MSNET_INTERFACE ms_s32 
	msnet_api_epollRecv(ms_s32 ms_in fd ,  ms_pu08 ms_in  pbuf, ms_u32 ms_in  size,ms_u32 ms_in  timeout);
MSNET_INTERFACE ms_s32 
	msnet_api_epollRecv3(ms_s32 ms_in fd ,  ms_pu08 ms_in  pbuf, ms_u32 ms_in  size) ;
MSNET_INTERFACE ms_s32 
	msnet_api_epollSend(ms_s32 ms_in fd ,  ms_pu08 ms_in  pbuf, ms_u32 ms_in  size,ms_u32 ms_in  timeout);
MSNET_INTERFACE ms_s32 
	msnet_api_epollSend2(ms_s32 ms_in fd ,  ms_pu08 ms_in  pbuf, ms_u32 ms_in  size,ms_u32 ms_in  timeout) ;
MSNET_INTERFACE ms_s32 
	msnet_api_epollSend3(ms_s32 ms_in fd ,  ms_pu08 ms_in  pbuf, ms_u32 ms_in  size) ;
MSNET_INTERFACE ms_u08 
	msnet_api_getNetmaskBitNum(ms_string ms_in pnetmask);
MSNET_INTERFACE ms_bool 
	msnet_api_getGatewayByNetmask(ms_string ms_in pipaddr,ms_string ms_in pnetmask,ms_string ms_out netmask_out,ms_string ms_out usr_gateway);
MSNET_INTERFACE ms_bool 
	msnet_api_subnet(ms_string ms_in pipaddr,ms_string ms_in pnetmask,ms_string ms_out netmask_out);
MSNET_INTERFACE ms_s32 
	msnet_api_getIpList(ms_string ms_in pipaddr_start,ms_string ms_in pipaddr_end,ms_string ms_out pmipaddrs,ms_u08 arrsize);
MSNET_INTERFACE ms_bool 
	msnet_api_isVaildFd(ms_s32 fd);
MSNET_INTERFACE NETCFG_MODE 
	msnet_api_getIpMode(ms_u08* ms_in mode_string);
MSNET_INTERFACE ms_string 
	msnet_api_net2str(ms_pbyte pbuf);
MSNET_INTERFACE ms_void 
	msnet_api_byte2str(ms_pbyte pbuf,ms_u32 len,ms_pbyte pstrout);
MSNET_INTERFACE ms_void 
	msnet_api_str2byte(ms_pbyte pstrin,ms_u32 len,ms_pbyte pbuf);
MSNET_INTERFACE ms_void 
	msnet_api_byte2hexStr(ms_pbyte pbuf,ms_u32 len,ms_pbyte pstrout);
MSNET_INTERFACE ms_bool 
	msnet_api_ping( ms_string ipaddr);

#define ms_net2str(pbyte)	msnet_api_net2str(pbyte)
#define ms_str2net(str)		inet_addr(str)
#define ms_n2hs(pbyte)		ntohs(pbyte) 
#define ms_h2ns(pbyte)		htons(pbyte) 
#define ms_n2hl(pbyte)		ntohl(pbyte) 
#define ms_h2nl(pbyte)		htonl(pbyte) 
#define ms_byte2str(pbuf,len,pstrout)		msnet_api_byte2str(pbuf,len,pstrout)
#define ms_byte2hexStr(pbuf,len,pstrout)	msnet_api_byte2hexStr(pbuf,len,pstrout)
#define ms_byte2macStr(pbuf,len,pstrout)	msnet_api_macByte2str(pbuf,len,pstrout)
#define ms_str2byte(pstrin,len,pbuf)		msnet_api_str2byte(pstrin,len,pbuf)
#endif

